package com.gmlive.common.sample

import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gmlive.common.sample.coroutines.CoroutinesActivity
import com.gmlive.common.sample.diagnose.NetworkDiagnoseActivity
import com.gmlive.common.sample.dialog.DialogDemoActivity
import com.gmlive.common.sample.highlight.HighlightActivity
import com.gmlive.common.sample.http.RetrofitActivity
import com.gmlive.common.sample.pay.PayActivity
import com.gmlive.common.sample.share.TestShareActivity
import com.gmlive.common.sample.update.AppUpdateActivity
import com.gmlive.common.sample.widgets.*
import com.gmlive.common.ui.app.IkCompatActivity
import com.gmlive.common.ui.widget.LinearItemDecoration
import com.meelive.ingkee.iknetwork.diagnose.impl.IKNetworkDiagnoseService
import kotlinx.android.synthetic.main.activity_main_component.*
import kotlinx.android.synthetic.main.recycler_component_item_widget.view.*

class ComponentActivity : IkCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_component)
        setSupportActionBar(toolbar)

        widgets_list.layoutManager = LinearLayoutManager(this)
        widgets_list.addItemDecoration(LinearItemDecoration(LinearItemDecoration.VERTICAL))

        widgets_list.adapter = WidgetsAdapter(createWidgetsComponent())
    }

    private fun createWidgetsComponent(): List<ItemBean> {
        val beans = mutableListOf<ItemBean>()
        beans.add(ItemBean("字体样式浏览", ComponentName(this, TextStyleActivity::class.java)))
        beans.add(ItemBean("按钮样式浏览", ComponentName(this, ButtonStyleActivity::class.java)))
        beans.add(ItemBean("弹框样式", ComponentName(this, DialogDemoActivity::class.java)))
        beans.add(ItemBean("沉浸式", ComponentName(this, StatusBarActivity::class.java)))
        beans.add(ItemBean("自定义ActionBar样式", ComponentName(this, ActionBarActivity::class.java)))
        beans.add(ItemBean("自定义标题栏", ComponentName(this, TitleBarActivity::class.java)))
        beans.add(ItemBean("旧的demo页面", ComponentName(this, MainActivity::class.java)))
        beans.add(ItemBean("图片加载样式", ComponentName(this, ImageLoadDemoActivity::class.java)))
        beans.add(ItemBean("retrofit 网络请求", ComponentName(this, RetrofitActivity::class.java)))
        beans.add(ItemBean("高亮", ComponentName(this, HighlightActivity::class.java)))
        beans.add(ItemBean("支付SDK示例", ComponentName(this, PayActivity::class.java)))
        beans.add(ItemBean("3D礼物播放效果", ComponentName(this, VideoGiftPlayerActivity::class.java)))
        beans.add(ItemBean("应用升级", ComponentName(this, AppUpdateActivity::class.java)))
        beans.add(ItemBean("分享SDK示例", ComponentName(this, TestShareActivity::class.java)))
        beans.add(ItemBean("SVGA", ComponentName(this, SVGAActivity::class.java)))
        beans.add(ItemBean("协程测试", ComponentName(this, CoroutinesActivity::class.java)))
        beans.add(ItemBean("XUI样式", ComponentName(this, XUIActivity::class.java)))
        beans.add(ItemBean("GridPagerDemo", ComponentName(this, RecyclerViewActivity::class.java)))
        beans.add(ItemBean("网络诊断", ComponentName(this, NetworkDiagnoseActivity::class.java)))
        beans.add(ItemBean("自定义TabLayout", ComponentName(this, BulgeTabActivity::class.java)))
        beans.add(ItemBean("悬浮框权限", ComponentName(this, AlertWindowActivity::class.java)))
        return beans
    }

    class WidgetsAdapter(private val beans: List<ItemBean>) : RecyclerView.Adapter<WidgetViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WidgetViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_component_item_widget, parent, false)
            return WidgetViewHolder(view)
        }

        override fun getItemCount(): Int {
            return beans.size
        }

        override fun onBindViewHolder(viewHolder: WidgetViewHolder, position: Int) {
            viewHolder.bindView(beans[position])
        }
    }

    class WidgetViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindView(bean: ItemBean) {
            itemView.title.text = bean.title
            itemView.setOnClickListener {
                it.context.startActivity(Intent().apply { component = bean.component })
            }
        }
    }

    data class ItemBean(val title: String, val component: ComponentName)
}